package com.dingding.service.activity.core.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.config.SystemConfig;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.dto.indexmodule.GetIndexModulesDto;
import com.dingding.facde.activity.dto.indexmodule.ReqGetIndexModules;
import com.dingding.facde.activity.dto.indexmodule.ResGetIndexModules;
import com.dingding.service.activity.core.biz.IndexModuleListService;
import com.dingding.service.activity.core.dao.IActivityDao;

/**
 * @Description:获取首页模块列表
 * @author fengshuonan
 * @date 2016年8月4日 下午3:15:24
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("indexModuleListService")
public class IndexModuleListServiceImpl extends AbstractResponse<ResGetIndexModules, ReqGetIndexModules> implements IndexModuleListService{

	@Resource(name = "activityDao")
	private IActivityDao activityDao;
	
	/**
	 * @Description:获取首页的模块列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	@Transactional(readOnly = true)
	public String getIndexModuleList(String clientData) {
		ResponseEntity<ResGetIndexModules> response = new ResponseEntity<ResGetIndexModules>();
		ReqGetIndexModules request = super.parseRequestJson(clientData);
		
		//验证参数是否为空
		if(!super.validateParam(response, request)){
			return responseBeanToJson(response);
		}
		
		//获取首页模块列表
		this.getModuleList(request.getCityId(),request.getUserId(),response);
		
		return super.responseBeanToJson(response);
	}
	
	/**
	 * @Description:获取首页的模块列表
	 * @author yhq
	 * @since 2.5.4
	 */
	@Override
	@Transactional(readOnly = true)
	public String getModuleList(String clientData) {
		ResponseEntity<ResGetIndexModules> response = new ResponseEntity<ResGetIndexModules>();
		ReqGetIndexModules request = super.parseRequestJson(clientData);
		
		//验证参数是否为空
		if(!super.validateParam(response, request)){
			return responseBeanToJson(response);
		}
		
		//获取首页模块列表
		this.getIndexModuleList(request.getCityId(),request.getUserId(),response);
		
		return super.responseBeanToJson(response);
	}
	
	/**
	 * @Description:获取首页服务列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	private void getIndexModuleList(String cityNo,String userId,ResponseEntity<ResGetIndexModules> response){
		List<GetIndexModulesDto> list = this.activityDao.getIndexModuleList(cityNo);
		if(list != null){
			//拼接url
			generateUrl(cityNo,userId,list);
			response.setResponseBody(new ResGetIndexModules(list));
			response.setCode(HttpStatus.SC_OK);
			response.setErrorMessage("请求成功！");
			response.setPromptMessage("请求成功");
		}else{
			response.setCode(HttpStatus.SC_BAD_GATEWAY);
			response.setErrorMessage("获取不到首页模块列表");
			response.setPromptMessage("获取不到首页模块列表");
		}
	}
	
	
	/**
	 * @Description:获取首页服务列表
	 * @author yhq
	 * @since 2.5.4
	 */
	private void getModuleList(String cityNo,String userId,ResponseEntity<ResGetIndexModules> response){
		List<GetIndexModulesDto> list = this.activityDao.getIndexModuleList(cityNo);
		if(list != null){
			
			for (int i = 0; i < list.size(); i++) {
				
				if(list.get(i).getUseType() > 4){
					list.remove(i);
				}
			}
			
			//拼接url
			generateUrl(cityNo,userId,list);
			response.setResponseBody(new ResGetIndexModules(list));
			response.setCode(HttpStatus.SC_OK);
			response.setErrorMessage("请求成功！");
			response.setPromptMessage("请求成功");
		}else{
			response.setCode(HttpStatus.SC_BAD_GATEWAY);
			response.setErrorMessage("获取不到首页模块列表");
			response.setPromptMessage("获取不到首页模块列表");
		}
	}
	
	/**
	 * @Description:完善返回给前台的url
	 * @author fengshuonan
	 * @since 2.4
	 */
	private void generateUrl(String cityNo,String userId,List<GetIndexModulesDto> list){
		for(GetIndexModulesDto dto : list){
			if(ToolUtil.isNotBlank(dto.getWebViewUrl())){
				dto.setWebViewUrl(SystemConfig.getAccess_url_not_port()+ dto.getWebViewUrl() + "?cityNo=" + cityNo + "&userId=" + userId);
			}
		}
	}
}
